import multer, { MulterError } from 'multer';
import { AVATAR_SIZE_LIMIT } from '../constants/normal.constants';

const fileFilter = (req: any, file: Express.Multer.File, cb: multer.FileFilterCallback) => {
    if (['image/jpeg', 'image/png', 'image/gif'].includes(file.mimetype)) {
        cb(null, true);
    } else {
        cb(new MulterError("LIMIT_UNEXPECTED_FILE"));
    }
};

const upload = multer({
    storage: multer.memoryStorage(),
    fileFilter,
    limits: { fileSize: AVATAR_SIZE_LIMIT }
});

const uploadAvatar = upload.single('file')

export default uploadAvatar